package com.suncaper.han_yi_system.controller;

import com.suncaper.han_yi_system.pojo.dto.LoginDto;
import com.suncaper.han_yi_system.result.Result;
import com.suncaper.han_yi_system.service.LoginService;
import com.suncaper.han_yi_system.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/acc")
@CrossOrigin(origins = "*")
@Tag(name = "用户登录相关接口")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public Result login(@RequestBody LoginDto loginDto, HttpServletRequest request) {
        Map<String, Object> data;
        try {
            data = loginService.login(loginDto);
        }catch (Exception e){
            return Result.error().message("用户名或密码错误");
        }
        return Result.ok().message("登录成功").data(data);
    }

    @Operation(summary = "用户登出")
    @GetMapping("/logout")
    public Result logout() {
        loginService.logout();
        return Result.ok().message("登出成功");
    }
}
